<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>prototype原型对象</title>
</head>

<body>
    <script>
        function Student(name, age) {
            this.name = name
            this.age = age
            this.sleep = function () {
                console.log('睡觉');
            }
        }

        const zhangsan = new Student('张三', 18)
        const lisi = new Student('李四', 18)

        // 不使用原型对象，每个实例对象会单独开辟一个新的空间
        console.log(zhangsan.sleep === lisi.sleep); // false

        // 使用原型对象挂载函数，所有实例化对象使用同一个函数，避免浪费内存
        Student.prototype.study = function () {
            console.log('学习');
        }

        console.log(zhangsan.study === lisi.study); // true
    </script>
</body>

</html>